package com.loveazure.ui;

import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.loveazure.bll.Handler;
import com.loveazure.eo.UserCourse;
import com.loveazure.ui.MainFrame.Status;
import com.loveazure.ui.action.DeleteUserCourseAction;
import com.loveazure.ui.action.ShuffleAction;

public class ConfigPanel extends IPanel {

	public UserCourseModel model;
	public JTable table;
	private JButton registerBtn;
	private JButton deleteBtn;
	private JButton startBtn;
	private JButton shuffleBtn;

	private Handler handler;

	// private JButton resetBtn;

	public ConfigPanel() {
		super();
	}

	@Override
	public void iLayout() {
		GridBagLayout gbl = new GridBagLayout();
		gbl.columnWidths = new int[] { 30, 80, 80, 80, 30 };
		gbl.columnWeights = new double[] { 0, 1, 1, 1, 0 };
		gbl.rowHeights = new int[] { 45, 200, 30, 45 };
		setLayout(gbl);
	}

	@Override
	public void iComponent() {
		registerBtn = new JButton("注册课程");
		registerBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				handler.getFrame().switchPanel(Status.COURSE);
			}
		});
		DeleteUserCourseAction deleteUserCourseAction = new DeleteUserCourseAction(
				this);
		deleteBtn = new JButton(deleteUserCourseAction);

		final ShuffleAction shuffleAction = new ShuffleAction(this);
		shuffleBtn = new JButton(shuffleAction);

		startBtn = new JButton("开始学习");
		startBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRow() == -1) {
					return;
				}
				handler.setCurrentCourse(model.getRow(table.getSelectedRow()));
				handler.getFrame().switchPanel(Status.STUDY);
			}
		});

		model = new UserCourseModel();
		table = new JTable(model);

		final JPopupMenu menu = new JPopupMenu();
		menu.add(shuffleAction);
		menu.add(deleteUserCourseAction);

		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					// 取得右键点击所在行
					int row = e.getY() / table.getRowHeight();
					table.setRowSelectionInterval(row, row);
					// 弹出菜单
					menu.show(e.getComponent(), e.getX(), e.getY());
				}
			}
		});
		table.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {

					@Override
					public void valueChanged(ListSelectionEvent e) {
						if (table.getSelectedRowCount() == 0) {
							return;
						}
						UserCourse row = model.getRow(table.getSelectedRow());
						if (row.getWordLearned().size() == 0) {
							// shuffleBtn.setEnabled(true);
							shuffleAction.setEnabled(true);
						} else {
							shuffleAction.setEnabled(false);
							// shuffleBtn.setEnabled(false);
						}
					}
				});

		JScrollPane scrolltable = new JScrollPane(table);

		add(new JLabel("请选择课程："), new GBC(1, 0, 1, 1));
		add(scrolltable, new GBC(1, 1, 3, 1).setFill(GBC.BOTH));
		add(registerBtn, new GBC(1, 2, 1, 1));
		add(deleteBtn, new GBC(2, 2, 1, 1));
		add(shuffleBtn, new GBC(3, 2, 1, 1));
		add(startBtn, new GBC(1, 3, 3, 1));
	}

	@Override
	public void iInit() {
		handler = Handler.getInstance();
		model.removeAll();

		UserCourse[] userCourse = handler.getOper().getAvailableCourse();
		if (userCourse != null && userCourse.length != 0) {
			for (UserCourse u : userCourse) {
				model.addrow(u);
			}
		}

	}
}
